package com.todo.message.application.backend.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.message.application.service.DataService;
import com.todo.message.iface.vo.DataParam;
import com.todo.message.iface.vo.amis.EchartsVo;
import com.todo.message.iface.vo.amis.SmsTimeLineVo;
import com.todo.message.iface.vo.amis.UserTimeLineVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Objects;

/**
 * 获取数据接口（全链路追踪)
 *
 * @author 3y
 */
@Slf4j
@RestController
@RequestMapping("/bd/trace")
@Tag(name = "获取数据接口（全链路追踪)")
public class DataController {
    @Autowired
    private DataService dataService;

    @PostMapping("/message")
    @Operation(summary = "/获取【72小时】发送消息的全链路数据")
    public BizResponse<UserTimeLineVo> getMessageData(@RequestBody DataParam dataParam) {
        if (Objects.isNull(dataParam) || CharSequenceUtil.isBlank(dataParam.getMessageId())) {
            return BizResponse.success(UserTimeLineVo.builder().items(new ArrayList<>()).build());
        }
        return BizResponse.success(dataService.getTraceMessageInfo(dataParam.getMessageId()));
    }

    @PostMapping("/user")
    @Operation(summary = "/获取【当天】用户接收消息的全链路数据")
    public BizResponse<UserTimeLineVo> getUserData(@RequestBody DataParam dataParam) {
        if (Objects.isNull(dataParam) || CharSequenceUtil.isBlank(dataParam.getReceiver())) {
            return BizResponse.success(UserTimeLineVo.builder().items(new ArrayList<>()).build());
        }
        return BizResponse.success(dataService.getTraceUserInfo(dataParam.getReceiver()));
    }

    @PostMapping("/messageTemplate")
    @Operation(summary = "/获取消息模板全链路数据")
    public BizResponse<EchartsVo> getMessageTemplateData(@RequestBody DataParam dataParam) {
        EchartsVo echartsVo = EchartsVo.builder().build();
        if (CharSequenceUtil.isNotBlank(dataParam.getBusinessId())) {
            echartsVo = dataService.getTraceMessageTemplateInfo(dataParam.getBusinessId());
        }
        return BizResponse.success(echartsVo);
    }

    @PostMapping("/sms")
    @Operation(summary = "/获取短信下发数据")
    public BizResponse<SmsTimeLineVo> getSmsData(@RequestBody DataParam dataParam) {
        if (Objects.isNull(dataParam) || Objects.isNull(dataParam.getDateTime()) || CharSequenceUtil.isBlank(dataParam.getReceiver())) {
            return BizResponse.success(SmsTimeLineVo.builder().items(Lists.newArrayList()).build());
        }
        return BizResponse.success(dataService.getTraceSmsInfo(dataParam));
    }

}
